/*
 * @(#)SysFunction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.commons.dbutils.DbUtils;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.bean.SysFunction;
import com.dcivision.framework.web.AbstractSearchForm;

/**
  SysFunctionDAObject.java

  This class is the data access bean for table "SYS_FUNCTION".

  @author      Angus Shiu
  @company     DCIVision Limited
  @creation date   15/07/2003
  @version     $Revision: 1.16.32.1 $
*/

public class SysFunctionDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.16.32.1 $";

  public static final String TABLE_NAME = "SYS_FUNCTION";

  public SysFunctionDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("FUNCTION_CODE");
    this.vecDBColumn.add("FUNCTION_NAME");
    this.vecDBColumn.add("FUNCTION_NAME_ZH_HK");
    this.vecDBColumn.add("FUNCTION_NAME_ZH_CN");
    this.vecDBColumn.add("PARENT_ID");
    this.vecDBColumn.add("LINK_URL");
    this.vecDBColumn.add("DISPLAY_SEQ");
    this.vecDBColumn.add("MENU_TYPE");
    this.vecDBColumn.add("PERMISSION_TYPE");
    this.vecDBColumn.add("STATUS");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.FUNCTION_CODE, A.FUNCTION_NAME, A.FUNCTION_NAME_ZH_HK, A.FUNCTION_NAME_ZH_CN, A.PARENT_ID, A.LINK_URL, A.DISPLAY_SEQ, A.MENU_TYPE, A.PERMISSION_TYPE, A.STATUS, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   SYS_FUNCTION A ");
        sqlStat.append("WHERE  A.ID = ? AND A.STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          SysFunction tmpSysFunction = new SysFunction();
          tmpSysFunction.setID(getResultSetInteger(rs, "ID"));
          tmpSysFunction.setFunctionCode(getResultSetString(rs, "FUNCTION_CODE"));
          tmpSysFunction.setFunctionName(getResultSetString(rs, "FUNCTION_NAME"));
          tmpSysFunction.setFunctionNameZhHk(getResultSetString(rs, "FUNCTION_NAME_ZH_HK"));
          tmpSysFunction.setFunctionNameZhCn(getResultSetString(rs, "FUNCTION_NAME_ZH_CN"));
          tmpSysFunction.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpSysFunction.setLinkUrl(getResultSetString(rs, "LINK_URL"));
          tmpSysFunction.setDisplaySeq(getResultSetInteger(rs, "DISPLAY_SEQ"));
          tmpSysFunction.setMenuType(getResultSetString(rs, "MENU_TYPE"));
          tmpSysFunction.setPermissionType(getResultSetString(rs, "PERMISSION_TYPE"));
          tmpSysFunction.setStatus(getResultSetString(rs, "STATUS"));
          tmpSysFunction.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpSysFunction.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpSysFunction.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpSysFunction.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpSysFunction.setCreatorName(UserInfoFactory.getUserFullName(tmpSysFunction.getCreatorID()));
          tmpSysFunction.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysFunction.getUpdaterID()));
          return(tmpSysFunction);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(preStat);
      }
    }
  }

  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.FUNCTION_CODE, A.FUNCTION_NAME, A.FUNCTION_NAME_ZH_HK, A.FUNCTION_NAME_ZH_CN, A.PARENT_ID, A.LINK_URL, A.DISPLAY_SEQ, A.MENU_TYPE, A.PERMISSION_TYPE, A.STATUS, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   SYS_FUNCTION A ");
        sqlStat.append("WHERE  A.STATUS = ? ");

        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
					String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());
        
        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          SysFunction tmpSysFunction = new SysFunction();
          tmpSysFunction.setID(getResultSetInteger(rs, "ID"));
          tmpSysFunction.setFunctionCode(getResultSetString(rs, "FUNCTION_CODE"));
          tmpSysFunction.setFunctionName(getResultSetString(rs, "FUNCTION_NAME"));
          tmpSysFunction.setFunctionNameZhHk(getResultSetString(rs, "FUNCTION_NAME_ZH_HK"));
          tmpSysFunction.setFunctionNameZhCn(getResultSetString(rs, "FUNCTION_NAME_ZH_CN"));
          tmpSysFunction.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpSysFunction.setLinkUrl(getResultSetString(rs, "LINK_URL"));
          tmpSysFunction.setDisplaySeq(getResultSetInteger(rs, "DISPLAY_SEQ"));
          tmpSysFunction.setMenuType(getResultSetString(rs, "MENU_TYPE"));
          tmpSysFunction.setPermissionType(getResultSetString(rs, "PERMISSION_TYPE"));
          tmpSysFunction.setStatus(getResultSetString(rs, "STATUS"));
          tmpSysFunction.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpSysFunction.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpSysFunction.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpSysFunction.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpSysFunction.setCreatorName(UserInfoFactory.getUserFullName(tmpSysFunction.getCreatorID()));
          tmpSysFunction.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysFunction.getUpdaterID()));
          tmpSysFunction.setRecordCount(totalNumOfRecord);
          tmpSysFunction.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpSysFunction);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(preStat);
        DbUtils.closeQuietly(rsCnt);
        DbUtils.closeQuietly(preStatCnt);
      }
    }
  }

  protected synchronized List getList() throws ApplicationException {
    java.sql.PreparedStatement preStat = null;
    java.sql.ResultSet rs = null;
    java.util.List sysFunctionList = new java.util.ArrayList();

    try{
      StringBuffer sqlStat = new StringBuffer();

      sqlStat.append("SELECT ID, FUNCTION_CODE, FUNCTION_NAME, FUNCTION_NAME_ZH_HK, FUNCTION_NAME_ZH_CN, PARENT_ID, LINK_URL, ");
      sqlStat.append("DISPLAY_SEQ, MENU_TYPE, PERMISSION_TYPE, STATUS, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE ");
      sqlStat.append("FROM SYS_FUNCTION ");
      sqlStat.append("WHERE STATUS = ? ");
      sqlStat.append("ORDER BY PARENT_ID ASC, DISPLAY_SEQ ASC ");
      preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
      this.setPrepareStatement(preStat, 1, GlobalConstant.STATUS_ACTIVE);
      rs = preStat.executeQuery();
      while (rs.next()) {
        SysFunction tmpSysFunction = new SysFunction();
        tmpSysFunction.setID(getResultSetInteger(rs, "ID"));
        tmpSysFunction.setFunctionCode(getResultSetString(rs, "FUNCTION_CODE"));
        tmpSysFunction.setFunctionName(getResultSetString(rs, "FUNCTION_NAME"));
        tmpSysFunction.setFunctionNameZhHk(getResultSetString(rs, "FUNCTION_NAME_ZH_HK"));
        tmpSysFunction.setFunctionNameZhCn(getResultSetString(rs, "FUNCTION_NAME_ZH_CN"));
        tmpSysFunction.setParentID(getResultSetInteger(rs, "PARENT_ID"));
        tmpSysFunction.setLinkUrl(getResultSetString(rs, "LINK_URL"));
        tmpSysFunction.setDisplaySeq(getResultSetInteger(rs, "DISPLAY_SEQ"));
        tmpSysFunction.setMenuType(getResultSetString(rs, "MENU_TYPE"));
        tmpSysFunction.setPermissionType(getResultSetString(rs, "PERMISSION_TYPE"));
        tmpSysFunction.setStatus(getResultSetString(rs, "STATUS"));
        tmpSysFunction.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
        tmpSysFunction.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
        tmpSysFunction.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
        tmpSysFunction.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
        tmpSysFunction.setCreatorName(UserInfoFactory.getUserFullName(tmpSysFunction.getCreatorID()));
        tmpSysFunction.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysFunction.getUpdaterID()));
        sysFunctionList.add(tmpSysFunction);
      }
      return (sysFunctionList);
    } catch (ApplicationException appEx) {
      throw appEx;
    } catch (SQLException sqle) {
      log.error(sqle, sqle);
      throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
    } catch (Exception e) {
      log.error(e, e);
      throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
    } finally {
      DbUtils.closeQuietly(rs);
      DbUtils.closeQuietly(preStat);
    }
  }

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    SysFunction tmpSysFunction = (SysFunction)((SysFunction)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   SYS_FUNCTION(ID, FUNCTION_CODE, FUNCTION_NAME, FUNCTION_NAME_ZH_HK, FUNCTION_NAME_ZH_CN, PARENT_ID, LINK_URL, DISPLAY_SEQ, MENU_TYPE, PERMISSION_TYPE, STATUS, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpSysFunction.getFunctionCode());
        setPrepareStatement(preStat, 3, tmpSysFunction.getFunctionName());
        setPrepareStatement(preStat, 4, tmpSysFunction.getFunctionNameZhHk());
        setPrepareStatement(preStat, 5, tmpSysFunction.getFunctionNameZhCn());
        setPrepareStatement(preStat, 6, tmpSysFunction.getParentID());
        setPrepareStatement(preStat, 7, tmpSysFunction.getLinkUrl());
        setPrepareStatement(preStat, 8, tmpSysFunction.getDisplaySeq());
        setPrepareStatement(preStat, 9, tmpSysFunction.getMenuType());
        setPrepareStatement(preStat, 10, tmpSysFunction.getPermissionType());
        setPrepareStatement(preStat, 11, tmpSysFunction.getStatus());
        setPrepareStatement(preStat, 12, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 13, currTime);
        setPrepareStatement(preStat, 14, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 15, currTime);
        preStat.executeUpdate();
        tmpSysFunction.setID(nextID);
        tmpSysFunction.setCreatorID(sessionContainer.getUserRecordID());
        tmpSysFunction.setCreateDate(currTime);
        tmpSysFunction.setUpdaterID(sessionContainer.getUserRecordID());
        tmpSysFunction.setUpdateDate(currTime);
        tmpSysFunction.setCreatorName(UserInfoFactory.getUserFullName(tmpSysFunction.getCreatorID()));
        tmpSysFunction.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysFunction.getUpdaterID()));
        return(tmpSysFunction);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        DbUtils.closeQuietly(preStat);
      }
    }
  }

  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    SysFunction tmpSysFunction = (SysFunction)((SysFunction)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE SYS_FUNCTION ");
        sqlStat.append("SET    FUNCTION_CODE=?, FUNCTION_NAME=?, FUNCTION_NAME_ZH_HK=?, FUNCTION_NAME_ZH_CN=?, PARENT_ID=?, LINK_URL=?, DISPLAY_SEQ=?, MENU_TYPE=?, PERMISSION_TYPE=?, STATUS=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpSysFunction.getFunctionCode());
        setPrepareStatement(preStat, 2, tmpSysFunction.getFunctionName());
        setPrepareStatement(preStat, 3, tmpSysFunction.getFunctionNameZhHk());
        setPrepareStatement(preStat, 4, tmpSysFunction.getFunctionNameZhCn());
        setPrepareStatement(preStat, 5, tmpSysFunction.getParentID());
        setPrepareStatement(preStat, 6, tmpSysFunction.getLinkUrl());
        setPrepareStatement(preStat, 7, tmpSysFunction.getDisplaySeq());
        setPrepareStatement(preStat, 8, tmpSysFunction.getMenuType());
        setPrepareStatement(preStat, 9, tmpSysFunction.getPermissionType());
        setPrepareStatement(preStat, 10, tmpSysFunction.getStatus());
        setPrepareStatement(preStat, 11, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 12, currTime);
        setPrepareStatement(preStat, 13, tmpSysFunction.getID());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpSysFunction.setUpdaterID(sessionContainer.getUserRecordID());
          tmpSysFunction.setUpdateDate(currTime);
          tmpSysFunction.setCreatorName(UserInfoFactory.getUserFullName(tmpSysFunction.getCreatorID()));
          tmpSysFunction.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysFunction.getUpdaterID()));
          return(tmpSysFunction);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        DbUtils.closeQuietly(preStat);
      }
    }
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    SysFunction tmpSysFunction = (SysFunction)((SysFunction)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   SYS_FUNCTION ");
        sqlStat.append("WHERE  ID=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpSysFunction.getID());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpSysFunction);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        DbUtils.closeQuietly(preStat);
      }
    }
  }

  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    SysFunction tmpSysFunction = (SysFunction)this.oldValue;

    if (tmpSysFunction != null) {
      oldValues.add(toAuditTrailValue(tmpSysFunction.getID()));
      oldValues.add(toAuditTrailValue(tmpSysFunction.getFunctionCode()));
      oldValues.add(toAuditTrailValue(tmpSysFunction.getFunctionName()));
      oldValues.add(toAuditTrailValue(tmpSysFunction.getFunctionNameZhHk()));
      oldValues.add(toAuditTrailValue(tmpSysFunction.getFunctionNameZhCn()));
      oldValues.add(toAuditTrailValue(tmpSysFunction.getParentID()));
      oldValues.add(toAuditTrailValue(tmpSysFunction.getLinkUrl()));
      oldValues.add(toAuditTrailValue(tmpSysFunction.getDisplaySeq()));
      oldValues.add(toAuditTrailValue(tmpSysFunction.getMenuType()));
      oldValues.add(toAuditTrailValue(tmpSysFunction.getPermissionType()));
      oldValues.add(toAuditTrailValue(tmpSysFunction.getStatus()));
      oldValues.add(toAuditTrailValue(tmpSysFunction.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpSysFunction.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpSysFunction.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpSysFunction.getUpdateDate()));
    }

    tmpSysFunction = (SysFunction)obj;
    if (tmpSysFunction != null) {
      newValues.add(toAuditTrailValue(tmpSysFunction.getID()));
      newValues.add(toAuditTrailValue(tmpSysFunction.getFunctionCode()));
      newValues.add(toAuditTrailValue(tmpSysFunction.getFunctionName()));
      newValues.add(toAuditTrailValue(tmpSysFunction.getFunctionNameZhHk()));
      newValues.add(toAuditTrailValue(tmpSysFunction.getFunctionNameZhCn()));
      newValues.add(toAuditTrailValue(tmpSysFunction.getParentID()));
      newValues.add(toAuditTrailValue(tmpSysFunction.getLinkUrl()));
      newValues.add(toAuditTrailValue(tmpSysFunction.getDisplaySeq()));
      newValues.add(toAuditTrailValue(tmpSysFunction.getMenuType()));
      newValues.add(toAuditTrailValue(tmpSysFunction.getPermissionType()));
      newValues.add(toAuditTrailValue(tmpSysFunction.getStatus()));
      newValues.add(toAuditTrailValue(tmpSysFunction.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpSysFunction.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpSysFunction.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpSysFunction.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/

  public List getListByParentID(Integer parentID, Integer userRecordID) throws ApplicationException {
    java.sql.PreparedStatement preStat = null;
    java.sql.ResultSet rs = null;
    java.util.List sysFunctionList = new java.util.ArrayList();
    try{
      StringBuffer sqlStat = new StringBuffer();

      sqlStat.append("SELECT SF.ID, SF.FUNCTION_CODE, SF.FUNCTION_NAME, SF.FUNCTION_NAME_ZH_HK, SF.FUNCTION_NAME_ZH_CN, SF.PARENT_ID, SF.LINK_URL, ");
      sqlStat.append("SF.DISPLAY_SEQ, SF.MENU_TYPE, SF.PERMISSION_TYPE, SF.STATUS, SF.CREATOR_ID, SF.CREATE_DATE, SF.UPDATER_ID, SF.UPDATE_DATE ");
      sqlStat.append("FROM SYS_FUNCTION SF LEFT JOIN USER_RECORD_PERMISSION UP ON UP.OBJECT_ID = SF.ID ");
      sqlStat.append("WHERE STATUS = ? AND PARENT_ID = ? AND USER_RECORD_ID = ? ");
      sqlStat.append("ORDER BY PARENT_ID ASC, DISPLAY_SEQ ASC ");
      preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
      this.setPrepareStatement(preStat, 1, GlobalConstant.STATUS_ACTIVE);
      this.setPrepareStatement(preStat, 2, parentID);
      this.setPrepareStatement(preStat, 3, userRecordID);
      rs = preStat.executeQuery();
      while (rs.next()) {
        SysFunction tmpSysFunction = new SysFunction();
        tmpSysFunction.setID(getResultSetInteger(rs, "ID"));
        tmpSysFunction.setFunctionCode(getResultSetString(rs, "FUNCTION_CODE"));
        tmpSysFunction.setFunctionName(getResultSetString(rs, "FUNCTION_NAME"));
        tmpSysFunction.setFunctionNameZhHk(getResultSetString(rs, "FUNCTION_NAME_ZH_HK"));
        tmpSysFunction.setFunctionNameZhCn(getResultSetString(rs, "FUNCTION_NAME_ZH_CN"));
        tmpSysFunction.setParentID(getResultSetInteger(rs, "PARENT_ID"));
        tmpSysFunction.setLinkUrl(getResultSetString(rs, "LINK_URL"));
        tmpSysFunction.setDisplaySeq(getResultSetInteger(rs, "DISPLAY_SEQ"));
        tmpSysFunction.setMenuType(getResultSetString(rs, "MENU_TYPE"));
        tmpSysFunction.setPermissionType(getResultSetString(rs, "PERMISSION_TYPE"));
        tmpSysFunction.setStatus(getResultSetString(rs, "STATUS"));
        tmpSysFunction.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
        tmpSysFunction.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
        tmpSysFunction.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
        tmpSysFunction.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
        tmpSysFunction.setCreatorName(UserInfoFactory.getUserFullName(tmpSysFunction.getCreatorID()));
        tmpSysFunction.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysFunction.getUpdaterID()));
        sysFunctionList.add(tmpSysFunction);
      }
      return (sysFunctionList);
    } catch (ApplicationException appEx) {
      throw appEx;
    } catch (SQLException sqle) {
      log.error(sqle, sqle);
      throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
    } catch (Exception e) {
      log.error(e, e);
      throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
    } finally {
      DbUtils.closeQuietly(rs);
      DbUtils.closeQuietly(preStat);
    }
  }
  
  public synchronized AbstractBaseObject getByFunctionCode(String functionCode) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.FUNCTION_CODE, A.FUNCTION_NAME, A.FUNCTION_NAME_ZH_HK, A.FUNCTION_NAME_ZH_CN, A.PARENT_ID, A.LINK_URL, A.DISPLAY_SEQ, A.MENU_TYPE, A.PERMISSION_TYPE, A.STATUS, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   SYS_FUNCTION A ");
        sqlStat.append("WHERE  A.FUNCTION_CODE = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, functionCode);
        rs = preStat.executeQuery();
        if (rs.next()) {
          SysFunction tmpSysFunction = new SysFunction();
          tmpSysFunction.setID(getResultSetInteger(rs, "ID"));
          tmpSysFunction.setFunctionCode(getResultSetString(rs, "FUNCTION_CODE"));
          tmpSysFunction.setFunctionName(getResultSetString(rs, "FUNCTION_NAME"));
          tmpSysFunction.setFunctionNameZhHk(getResultSetString(rs, "FUNCTION_NAME_ZH_HK"));
          tmpSysFunction.setFunctionNameZhCn(getResultSetString(rs, "FUNCTION_NAME_ZH_CN"));
          tmpSysFunction.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpSysFunction.setLinkUrl(getResultSetString(rs, "LINK_URL"));
          tmpSysFunction.setDisplaySeq(getResultSetInteger(rs, "DISPLAY_SEQ"));
          tmpSysFunction.setMenuType(getResultSetString(rs, "MENU_TYPE"));
          tmpSysFunction.setPermissionType(getResultSetString(rs, "PERMISSION_TYPE"));
          tmpSysFunction.setStatus(getResultSetString(rs, "STATUS"));
          tmpSysFunction.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpSysFunction.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpSysFunction.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpSysFunction.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpSysFunction.setCreatorName(UserInfoFactory.getUserFullName(tmpSysFunction.getCreatorID()));
          tmpSysFunction.setUpdaterName(UserInfoFactory.getUserFullName(tmpSysFunction.getUpdaterID()));
          return(tmpSysFunction);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(preStat);
      }
    }
  }

}
